package it.polimi.swKnights.SWIMv2.servlets.messages;

import it.polimi.swKnights.SWIMv2.ejb.messages.MessageHandlerBeanRemote;
import it.polimi.swKnights.SWIMv2.servlets.SwimServlet;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class WriteController
 */
public class WriteController extends SwimServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		request.setAttribute("write", true);
		String idParam = request.getParameter("id");
		

		if (idParam != null)
		{
			try
			{
				HttpSession session = request.getSession();
				int id = Integer.parseInt(idParam);
				String userName = (String) session.getAttribute("userName");
				String password = (String) session.getAttribute("password");
				
				MessageHandlerBeanRemote messageHandler = this
						.lookUp(MessageHandlerBeanRemote.class);
				Map<String, String> message = messageHandler.getMessage(userName, password, id);
				request.setAttribute("messageRecipient", message.get("sender"));
				request.setAttribute("messageObject", "RE: " + message.get("object"));
				String quote = "\r\n\r\n-- Received Message -- \r\n";
				request.setAttribute("messageText", quote + message.get("text"));
			}
			catch (Exception e)
			{
			}

		}

		request.getRequestDispatcher("messageHome").forward(request, response);
	}

}
